package com.project.service;


import com.project.pojo.Library;
import com.project.mapper.*;
import com.project.until.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;


import java.util.List;

public class LibraryService  {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


    /**
     * 查询所有
     * @return
     */
    public List<Library> selectAll(){
        //调用LibraryMapper.selectAll()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        LibraryMapper mapper = sqlSession.getMapper(LibraryMapper.class);

        //4. 调用方法
        List<Library> libraries = mapper.selectAll();

        sqlSession.close();

        return libraries;
    }

    /**
     *
     * @param id
     * @return
     */
    public Library selectById(int id){
        //调用BrandMapper.selectAll()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        LibraryMapper mapper = sqlSession.getMapper(LibraryMapper.class);

        //4. 调用方法
        Library library = mapper.selectById(id);

        sqlSession.close();

        return library;
    }
    public void add(Library library){

        SqlSession sqlSession = factory.openSession();

        LibraryMapper mapper = sqlSession.getMapper(LibraryMapper.class);

        mapper.add(library);

        sqlSession.commit();

        sqlSession.close();

    }

    public void deleteByid(Library library){

        SqlSession sqlSession = factory.openSession();

        LibraryMapper mapper = sqlSession.getMapper(LibraryMapper.class);

        mapper.deleteByid(library);

        sqlSession.commit();

        sqlSession.close();
    }

    public void update(Library library){

        SqlSession sqlSession = factory.openSession();

        LibraryMapper mapper = sqlSession.getMapper(LibraryMapper.class);

        mapper.update(library);

        sqlSession.commit();

        sqlSession.close();


    }
}
